package com.htl.exampro.user.controller;

import com.htl.exampro.base.exception.ExamProException;
import com.htl.exampro.base.model.PageResult;
import com.htl.exampro.base.model.ServerResponse;
import com.htl.exampro.user.pojo.query.UserPageParam;
import com.htl.exampro.user.pojo.vo.PasswordVO;
import com.htl.exampro.user.pojo.vo.UserVO;
import com.htl.exampro.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@Tag(name = "UserController", description = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 方法级文档注解
    @Operation(summary = "分页查询用户列表", description = "支持关键字搜索")
    @Parameter(name = "pageParam", description = "搜索关键字")
    @PostMapping("/page")
    public ServerResponse<PageResult<UserVO>> getUserList(@RequestBody UserPageParam pageParam) {
        PageResult<UserVO> result = userService.getUserList(pageParam);
        return ServerResponse.success(result);
    }

    @Operation(summary = "创建用户")
    @Parameter(name = "userVO", description = "用户实体")
    @PostMapping("register")
    public ServerResponse<Void> register(@RequestBody UserVO userVO) {
        userService.createUser(userVO);
        return ServerResponse.success();
    }

    @Operation(summary = "通过userIdList批量查询用户名")
    @Parameter(name = "userIdList", description = "用户ID")
    @PostMapping("/getUsernameList")
    public ServerResponse<List<UserVO>> getUsernameList(@RequestBody List<String> userIdList) {
        if (userIdList == null || userIdList.isEmpty())
            ExamProException.cast("userIdList入参不能为空!");

        List<UserVO> userVOS = userService.getUsernameList(userIdList);
        return ServerResponse.success(userVOS);
    }

    @Operation(summary = "获取用户详情")
    @Parameter(name = "id", description = "用户ID")
    @GetMapping("/{id}")
    public ServerResponse<UserVO> getUserDetail(@PathVariable Long id) {
        UserVO user = userService.getUserDetail(id);
        return ServerResponse.success(user);
    }

    @Operation(summary = "更新用户信息")
    @Parameter(name = "user", description = "用户实体")
    @PutMapping("updateUser")
    public ServerResponse<Void> updateUser(@RequestBody UserVO user) {
        userService.updateUser(user);
        return ServerResponse.success();
    }

    @Operation(summary = "管理员后台新增用户")
    @Parameter(name = "user", description = "用户实体")
    @PostMapping("addUser")
    public ServerResponse<Void> addUser(@RequestBody UserVO user) {
        userService.addUser(user);
        return ServerResponse.success();
    }


    @Operation(summary = "更新用户状态信息")
    @Parameter(name = "user", description = "用户实体")
    @PutMapping("/status")
    public ServerResponse<Void> updateUserStatus(@RequestBody UserVO user) {
        userService.updateUserStatus(user);
        return ServerResponse.success();
    }

    @Operation(summary = "删除用户")
    @Parameter(name = "id", description = "用户ID")
    @DeleteMapping("/{id}")
    public ServerResponse<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return ServerResponse.success();
    }

    @Operation(summary = "用户登录")
    @Parameter(name = "user", description = "用户实体")
    @PostMapping("/login")
    public ServerResponse<UserVO> login(@RequestBody UserVO user, HttpServletRequest request) {
        UserVO userVO = userService.login(user.getUsername(), user.getPassword(), request.getRemoteAddr());
        return ServerResponse.success(userVO);
    }

    @Operation(summary = "用户登出")
    @GetMapping("/logout")
    public ServerResponse<Void> logout() {
        userService.logout();
        return ServerResponse.success();
    }

    @Operation(summary = "修改密码")
    @Parameter(name = "password", description = "密码实体")
    @PutMapping("/password")
    public ServerResponse<Void> changePassword(@Validated @RequestBody PasswordVO passwordVO) {
        userService.changePassword(passwordVO.getOldPassword(), passwordVO.getNewPassword());
        return ServerResponse.success();
    }

    @Operation(summary = "重置用户密码")
    @Parameter(name = "id", description = "用户ID")
    @PutMapping("/{id}/password/reset")
    public ServerResponse<Void> resetPassword(@PathVariable("id") String id, @RequestBody PasswordVO passwordVO) {
        userService.resetPassword(id, passwordVO.getNewPassword());
        return ServerResponse.success();
    }


    @Operation(summary = "发送验证码")
    @PostMapping("/sendCode")
    public ServerResponse<Void> sendCode(@RequestBody UserVO userVO) {
        userService.sendCode(userVO);
        return ServerResponse.success();
    }

    @Operation(summary = "获取当前用户信息")
    @GetMapping("/current")
    public ServerResponse<UserVO> getCurrentUser() {
        UserVO user = userService.getCurrentUser();
        return ServerResponse.success(user);
    }

    @Operation(summary = "更新当前用户信息")
    @Parameter(name = "user", description = "用户实体")
    @PutMapping("/current")
    public ServerResponse<Void> updateCurrentUser(@RequestBody UserVO user) {
        userService.updateCurrentUser(user);
        return ServerResponse.success();
    }
}